/**
 * 流程实例管理 API
 */
import type {
  HistoricActivity,
  ProcessInstance,
  ProcessInstanceQuery,
} from './types';

import { requestClient } from '../request';

/**
 * 获取流程实例列表
 */
export async function getProcessInstanceListApi(
  params: ProcessInstanceQuery,
) {
  return requestClient.get<{
    list: ProcessInstance[];
    total: number;
    pageNum: number;
    pageSize: number;
  }>('/workflow/api/instance/list', { params });
}

/**
 * 获取流程实例详情
 */
export async function getProcessInstanceDetailApi(id: string) {
  return requestClient.get<ProcessInstance>(`/workflow/api/instance/${id}`);
}

/**
 * 启动流程实例
 */
export async function startProcessInstanceApi(data: {
  processDefinitionKey: string;
  businessKey?: string;
  variables?: Record<string, any>;
  name?: string;
}) {
  return requestClient.post<ProcessInstance>(
    '/workflow/api/instance/start',
    data,
  );
}

/**
 * 挂起流程实例
 */
export async function suspendProcessInstanceApi(id: string) {
  return requestClient.post(`/workflow/api/instance/${id}/suspend`);
}

/**
 * 激活流程实例
 */
export async function activateProcessInstanceApi(id: string) {
  return requestClient.post(`/workflow/api/instance/${id}/activate`);
}

/**
 * 删除流程实例
 */
export async function deleteProcessInstanceApi(
  id: string,
  deleteReason?: string,
) {
  return requestClient.delete(`/workflow/api/instance/${id}`, {
    params: { deleteReason },
  });
}

/**
 * 获取流程实例的流程图（高亮当前节点）
 */
export async function getProcessInstanceDiagramApi(id: string) {
  return requestClient.get<{
    bpmnXml: string;
    activeActivityIds: string[];
    finishedActivityIds: string[];
  }>(`/workflow/api/instance/${id}/diagram`);
}

/**
 * 获取流程实例的历史活动
 */
export async function getProcessInstanceHistoryApi(id: string) {
  return requestClient.get<HistoricActivity[]>(
    `/workflow/api/instance/${id}/history`,
  );
}

/**
 * 获取流程实例的变量
 */
export async function getProcessInstanceVariablesApi(id: string) {
  return requestClient.get<Record<string, any>>(
    `/workflow/api/instance/${id}/variables`,
  );
}

/**
 * 设置流程实例的变量
 */
export async function setProcessInstanceVariablesApi(
  id: string,
  variables: Record<string, any>,
) {
  return requestClient.post(
    `/workflow/api/instance/${id}/variables`,
    variables,
  );
}

